#include "EventItem.h"

#include <object_v2/object_v2-type.h>

struct _EventItem
{
        GtkBox parent_instance;

        /*模板部件*/
        GtkBox    *index_box;
        GtkLabel  *index_label;
        GtkFrame  *info_frame;
        GtkBox    *info_box;
        GtkLabel  *title_info;
        GtkLabel  *content_info;
        GtkBox    *title_box;
        GtkBox    *content_box;
        GtkBox    *date_box;
        GtkLabel  *date_label;
        GtkButton *revise_button;
        GtkButton *remove_button;

        /*栈区*/
        char date_str[7];
};

G_DEFINE_FINAL_TYPE(EventItem, event_item, GTK_TYPE_BOX)

static void
event_item_finalize(GObject *object)
{
        G_OBJECT_CLASS(event_item_parent_class)->finalize(object);
}

static void
iter_remove(EventItem *self)
{
        /*待处理*/
}

static void
event_item_class_init(EventItemClass *klass)
{
        GObjectClass   *object_class = G_OBJECT_CLASS(klass);
        GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);

        gtk_widget_class_set_template_from_resource(
                widget_class,
                "/org/korack/event/EventItem.ui");
        gtk_widget_class_bind_template_child(widget_class,
                                             EventItem,
                                             index_box);
        gtk_widget_class_bind_template_child(widget_class,
                                             EventItem,
                                             index_label);
        gtk_widget_class_bind_template_child(widget_class,
                                             EventItem,
                                             info_frame);
        gtk_widget_class_bind_template_child(widget_class, EventItem, info_box);
        gtk_widget_class_bind_template_child(widget_class,
                                             EventItem,
                                             title_info);
        gtk_widget_class_bind_template_child(widget_class,
                                             EventItem,
                                             content_info);
        gtk_widget_class_bind_template_child(widget_class,
                                             EventItem,
                                             title_box);
        gtk_widget_class_bind_template_child(widget_class,
                                             EventItem,
                                             content_box);
        gtk_widget_class_bind_template_child(widget_class, EventItem, date_box);
        gtk_widget_class_bind_template_child(widget_class,
                                             EventItem,
                                             date_label);
        gtk_widget_class_bind_template_child(widget_class,
                                             EventItem,
                                             revise_button);
        gtk_widget_class_bind_template_child(widget_class,
                                             EventItem,
                                             remove_button);

        gtk_widget_class_bind_template_callback(widget_class, iter_remove);

        object_class->finalize = event_item_finalize;
}

EventItem *
event_item_new()
{
        return g_object_new(EVENT_TYPE_ITEM, NULL);
}

static void
set_color(EventItem *self, int index)
{
        const char *css[][2] = {
                {   "item-color-red", NULL},
                {"item-color-yellow", NULL},
                { "item-color-green", NULL},
        };

        const char *title_css[][2] = {
                {   "title-color-red", NULL},
                {"title-color-yellow", NULL},
                { "title-color-green", NULL},
        };

        const char *css_with_radius[][3] = {
                {   "item-color-red", "radius10px", NULL},
                {"item-color-yellow", "radius10px", NULL},
                { "item-color-green", "radius10px", NULL},
        };

        gtk_widget_set_css_classes(GTK_WIDGET(self->title_box),
                                   title_css[index]);
        gtk_widget_set_css_classes(GTK_WIDGET(self->content_box), css[index]);
        gtk_widget_set_css_classes(GTK_WIDGET(self->date_box),
                                   css_with_radius[index]);
        gtk_widget_set_css_classes(GTK_WIDGET(self->index_box),
                                   css_with_radius[index]);
}

void
event_item_set_info(EventItem  *self,
                    gint        index,
                    const char *title,
                    const char *content,
                    const char *date_no_format)
{
        char     buf[5];
        GString *_date_str;

        if (index == 1)
                set_color(self, 0);
        else if (index < 3)
                set_color(self, 1);
        else
                set_color(self, 2);

        sprintf(buf, "%d", index);

        gtk_label_set_label(self->index_label, buf);
        gtk_label_set_label(self->title_info, title);
        gtk_label_set_label(self->content_info, content);

        _date_str = g_string_new("");
        g_string_append_len(_date_str, date_no_format, 4);
        g_string_append(_date_str, "年");
        g_string_append_len(_date_str, date_no_format + 4, 2);
        g_string_append(_date_str, "月");
        g_string_append_len(_date_str, date_no_format + 6, 2);
        g_string_append(_date_str, "日");

        memcpy(self->date_str, date_no_format, 6);

        gtk_label_set_label(self->date_label, _date_str->str);

        g_string_free(_date_str, true);
}

static void
event_item_init(EventItem *self)
{
        gtk_widget_init_template(GTK_WIDGET(self));

        memset(self->date_str, 0, 7);
}
